package com.zhsource.article.controller;

import com.github.pagehelper.PageInfo;
import com.zhsource.article.domain.Article;
import com.zhsource.article.query.ArticleQuery;
import com.zhsource.article.service.IArticleService;
import com.zhsource.auth.PermissionScanAnno;
import com.zhsource.exception.GlobalException;
import com.zhsource.tourist.dto.ArticleCountDto;
import com.zhsource.utils.R;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import io.swagger.annotations.ApiParam;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.util.Collections;
import java.util.List;
import java.util.Map;

/**
 * <p>@ClassName：ArticleController
 * <p>@Date 2023/10/17  10:21
 *
 * @Author zzl
 **/
@Slf4j
@Api(tags = "文章模块控制器")
@RestController
@RequestMapping("/article")
public class ArticleController {

    @Autowired
    private IArticleService articleService;

    // 查询所有

    @ApiOperation("查询所有")
    @GetMapping
    public R<List<Article>> list() {
        try {
            return R.ok("查询所有成功!", articleService.list());
        } catch (GlobalException e) {
            log.error("Article模块的查询所有发生异常,异常信息为:{}", e.getMessage());
            return R.fail(e.getMessage());
        } catch (Exception e) {
            log.error("Article模块的查询所有发生异常,异常信息为:{}", e.getMessage());
            return R.fail("系统异常,请重试");
        }
    }
    // 查询单个

    @GetMapping("/{id}")
    @ApiOperation(value = "根据Id获取文章信息")
    @PermissionScanAnno( name = "根据Id查询" , sn = "article:get")
    public R<Article> selectById(@ApiParam(value = "文章ID",readOnly = true) @PathVariable("id") Long id) {
        try {
            return R.ok("查询单个成功!", articleService.selectById(id));
        } catch (GlobalException e) {
            log.error("Article模块的查询单个发生异常,异常信息为:{}", e.getMessage());
            return R.fail(e.getMessage());
        } catch (Exception e) {
            log.error("Article模块的查询单个发生异常,异常信息为:{}", e.getMessage());
            return R.fail("系统异常,请重试");
        }
    }

    // 分页+ 模糊查询
    @PostMapping("/page")
    public R<PageInfo<Article>> page(@RequestBody ArticleQuery articleQuery ) {
        try {
            return R.ok("分页查询成功!", articleService.page(articleQuery));
        } catch (GlobalException e) {
            log.error("Article模块的分页查询发生异常,异常信息为:{}", e.getMessage());
            return R.fail(e.getMessage());
        } catch (Exception e) {
            log.error("Article模块的分页查询发生异常,异常信息为:{}", e.getMessage());
            return R.fail("系统异常,请重试");
        }
    }


    // 修改
    @PutMapping
    public R<Void> update(@RequestBody Article article ) {
        try {
            articleService.update(article);
            return R.ok("修改数据成功!");
        } catch (GlobalException e) {
            log.error("Article模块的修改发生异常,异常信息为:{}", e.getMessage());
            return R.fail(e.getMessage());
        } catch (Exception e) {
            log.error("Article模块的修改发生异常,异常信息为:{}", e.getMessage());
            return R.fail("系统异常,请重试");
        }
    }

    // 新增

    @PostMapping()
    public R<Void> insert(@RequestBody Article article ) {
        try {
            articleService.insert(article);
            return R.ok("新增数据成功!");
        } catch (GlobalException e) {
            log.error("Article模块的新增发生异常,异常信息为:{}", e.getMessage());
            return R.fail(e.getMessage());
        } catch (Exception e) {
            log.error("Article模块的新增发生异常,异常信息为:{}", e.getMessage());
            return R.fail("系统异常,请重试");
        }
    }
    // 删除单个

    @DeleteMapping("/{id}")
    public R<Void> delete(@PathVariable("id") Long id) {
        try {
            articleService.delete(id);
            return R.ok("删除单个成功!");
        } catch (GlobalException e) {
            log.error("Article模块的删除单个发生异常,异常信息为:{}", e.getMessage());
            return R.fail(e.getMessage());
        } catch (Exception e) {
            log.error("Article模块的查询删除发生异常,异常信息为:{}", e.getMessage());
            return R.fail("系统异常,请重试");
        }
    }

    // 批量删除
    @PatchMapping("/batchDel")
    public R<Void> batchDel(@RequestBody List<Long> ids) {
        try {
            articleService.batchDel(ids);
            return R.ok("批量删除成功!");
        } catch (GlobalException e) {
            log.error("Article模块的批量删除发生异常,异常信息为:{}", e.getMessage());
            return R.fail(e.getMessage());
        } catch (Exception e) {
            log.error("Article模块的批量删除发生异常,异常信息为:{}", e.getMessage());
            return R.fail("系统异常,请重试");
        }
    }

    @GetMapping("/chartColumn")
    public List<Article> getEchartsData() {
        return articleService.getEchartsData();
    }

    @GetMapping("/chartPie")
    public List<ArticleCountDto> getPieChartData() {
        try {
            return articleService.getPieChartData();
        } catch (GlobalException e) {
            log.error("Article模块的获取饼图数据发生异常,异常信息为:{}", e.getMessage());
            return Collections.emptyList();
        } catch (Exception e) {
            log.error("Article模块的获取饼图数据发生异常,异常信息为:{}", e.getMessage());
            return Collections.emptyList();
        }
    }
}
